//
//  XLStatusRetweetView.swift
//  XLVeibo
//
//  Created by macbook on 16/6/30.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit

class XLStatusRetweetView: UIView {

    var retweetViewBottomConstraint :Constraint?
    
    var status:XLStatus?{
    
        didSet{
           //转发微博的内容
//            contentLabel.text = status?.retweeted_status?.text
            contentLabel.attributedText = status?.retweeted_status?.textAttr
            
            //卸载约束
            retweetViewBottomConstraint?.uninstall()
            //有配图
            if status?.retweeted_status?.pic_urls?.count > 0 {
                
                //显示配图
                pictureRetweetView.hidden = false
               //赋值
                pictureRetweetView.pic_urls = status?.retweeted_status?.pic_urls
                
                //更新约束
               
                self.snp_updateConstraints(closure: { (make) in
                    
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureRetweetView).offset(OriginalMargin).constraint
                })
                
            }else
            {
                //没有配图
                pictureRetweetView.hidden = true
                
                //更新约束
                
                self.snp_updateConstraints(closure: { (make) in
                    
                     retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(OriginalMargin).constraint
                })

            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI
    private func setupUI(){
       
         backgroundColor = RBG(237, green: 237, blue: 237)
        addSubview(contentLabel)
        addSubview(pictureRetweetView)
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(self).offset(OriginalMargin)
            make.top.equalTo(self).offset(OriginalMargin)
        }
        
        pictureRetweetView.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(OriginalMargin)
            make.left.equalTo(contentLabel)
           
        }
        
        
        //给自己添加约束
        self.snp_makeConstraints { (make) in
            
           retweetViewBottomConstraint = make.bottom.equalTo(pictureRetweetView).offset(OriginalMargin).constraint
        }
    }
    
    
    
    //懒加载创建label
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, labelWith: KScreenW - 2 * OriginalMargin)
    
    //创建配图
    private lazy var pictureRetweetView:XLPictureView = XLPictureView()
}







